﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Election.BanToChucBinhBau
{
    public partial class ThemUngCuVienUc : UserControl
    {
        #region Attributes
        private string _imageSrcPath;
        private string _imageFileName;
        private string _id = null;
        private string _workingExperience;
        private string _ucvID;
        private string _memberName = null;
        
        public string imageSrcPath
        {
            get
            {
                return this._imageSrcPath;
            }
            set
            {
                this._imageSrcPath = value;
            }
        }
        public string id
        {
            get { return _id; }
            set { this._id = value; }
        }
        public string workingExperience
        {
            get 
            {
                return this.workingExRTxtBox.Text; 
            }
            set 
            { 
                this._workingExperience = value; 
            }
        }
        public string ucvID
        {
            get
            {
                return this._ucvID;
            }
            set
            {
                this._ucvID = value;
            }
        }
        public string memberName
        {
            get { return this._memberName; }
            set { this._memberName = value; }
        }
        public string imageFileName
        {
            get { return this._imageFileName; }
            set { this._imageFileName = value; }
        }
        #endregion

        #region Methods
        public ThemUngCuVienUc()
        {
            InitializeComponent();
        }

        public ThemUngCuVienUc(string id)
        {
            InitializeComponent();
            this.id = id;
        }

        private void addImageBut_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            //fd.Filter = "JPG Image|*.jpg|PNG Image|*.png, *.jpg|Bitmap Image|*.bmp|GIF Image|*.gif";
            fd.Filter = "JPG Image|*.jpg|PNG Image|*.png";
            fd.Title = "Save an image file";
            fd.ShowDialog();
            this.imageSrcPath = fd.FileName;
            if (this.imageSrcPath != "")
            {  
                this.pictureBox.Image = Image.FromFile(this.imageSrcPath);
                this.imageFileName = fd.SafeFileName;
            }
        }
        #endregion

        private void ThemUngCuVienUc_Load(object sender, EventArgs e)
        {
            if (this.id == null)
            {
                return;
            }
            string query = "select tv.*, dv.TENDV from quanlybaucu.thanhvien tv join quanlybaucu.donvi dv on (tv.thuocdonvi = dv.madv) where tv.matv = '" + this.id + "' ";
            try
            {
                DataTable thanhVien = orcl.ExecuteQueryThrowEx(query);
                if (thanhVien.Rows.Count == 0)
                {
                    MessageBox.Show("Hệ thống không có thông tin thành viên này");
                    return;
                }
                this.maTVLbl.Text = this.id;
                this.tenLbl.Text = thanhVien.Rows[0]["TEN"].ToString();
                this.phaiLbl.Text = thanhVien.Rows[0]["PHAI"].ToString();
                this.ngaySinhLbl.Text = thanhVien.Rows[0]["NGAYSINH"].ToString();
                this.queQuanLbl.Text = thanhVien.Rows[0]["QUEQUAN"].ToString();
                this.quocTichLbl.Text = thanhVien.Rows[0]["QUOCTICH"].ToString();
                this.dcThuongTruLbl.Text = thanhVien.Rows[0]["DCTHUONGTRU"].ToString();
                this.dcTamTruLbl.Text = thanhVien.Rows[0]["DCTAMTRU"].ToString();
                this.donViLbl.Text = thanhVien.Rows[0]["TENDV"].ToString();
                this.chiNhanhLbl.Text = thanhVien.Rows[0]["CHINHANH"].ToString();
                this.memberName = this.tenLbl.Text;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public bool ungCuVienInfoValidation()
        {
            if (this.idTxtBox.Text == null || this.idTxtBox.Text.Trim() == "")
            {
                MessageBox.Show("Mời bạn nhập mã ứng cử viên");
                return false;
            }
            this.ucvID = this.idTxtBox.Text;
            this.workingExperience = this.workingExRTxtBox.Text;
            return true;
        }

        private void removeImageBut_Click(object sender, EventArgs e)
        {
            this.pictureBox.Image = null;
            this.imageFileName = null;
            this.imageSrcPath = null;
        }
    }
}
